package io.renren.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;

import io.renren.common.validator.Assert;
import io.renren.dao.JxStudentDao;
import io.renren.entity.JxStudentEntity;


import io.renren.entity.TokenEntity;
import io.renren.form.LoginForm;
import io.renren.service.JxStudentService;
import io.renren.service.TokenService;
import io.renren.shiro.ShiroUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("jxStudentService")
public class JxStudentServiceImpl extends ServiceImpl<JxStudentDao, JxStudentEntity> implements JxStudentService {

    private final  static String DEFIND_PASSWORD="123456";

    @Autowired
    private TokenService tokenService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveStudent(JxStudentEntity st){
     st.setCreateTime(new Date());
        String salt = RandomStringUtils.randomAlphanumeric(20);
        if(null==st.getPassword()){
         st.setPassword(DEFIND_PASSWORD);
        }
        st.setSalt(salt);
        st.setPassword(ShiroUtils.sha256(st.getPassword(), st.getSalt()));
    return this.save(st);
    }

    @Override
    public PageUtils getAllWithPage(Map<String, Object> params) {

        PageHelper.startPage(Integer.parseInt(params.get("page").toString()),Integer.parseInt(params.get("limit").toString()));
        List<JxStudentEntity> list = this.baseMapper.getAllWithPage(params);
        PageInfo<JxStudentEntity> pageInfo = new PageInfo<>(list);
        return new PageUtils(list,(int)pageInfo.getTotal(),pageInfo.getPageSize(),pageInfo.getPageNum());
    }

    @Override
    public int addBatchStudent(List<JxStudentEntity> list) {
        return this.baseMapper.addBatchStudent(list);
    }

    @Override
    public List<JxStudentEntity> getALLlist() {
        return  this.baseMapper.getAllWithPage();
    }

    @Override
    public Map<String, Object> login(LoginForm form) {
        Map<String, Object> params=new HashMap<String, Object>();
        params.put("studentNum",form.getStudentNum());
        List<JxStudentEntity> list = this.baseMapper.getAllWithPage(params);
        Map<String, Object> map = new HashMap<>();
        if(list.size()>0) {
            JxStudentEntity entity = list.get(0);
            Assert.isNull(entity, "学号或密码错误");

            //密码错误
            if (!entity.getPassword().equals(ShiroUtils.sha256(form.getPassword(), entity.getSalt()))) {
                throw new RRException("手机号或密码错误");
            }

            //获取登录token
            TokenEntity tokenEntity = tokenService.createToken(entity.getId());
            map.put("token", tokenEntity.getToken());
            map.put("expire", tokenEntity.getExpireTime().getTime() - System.currentTimeMillis());
            entity.setId(null);
            map.put("student",entity);
            return  map;
        }else{
            Assert.isNull(null, "学号或密码错误");
        }

        return map;
    }
}
